import User from "../../../utils/user";
import { JoinActivity, CancelJoin, GetStudentData, activityExportJoin, activityExportSign, GetActivityDetail } from "../../../utils/request/api";
import msg from "../../../utils/msg";
var user = new User;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    content: {
      id: 0 as number,
      title: '' as string,
      content: '' as string,
      people: '' as string,
      place: '' as string,
      start_time: '' as string,
      end_time: '' as string,
      need: 0 as number,
      count:0 as number,
      lat:0 as number,
      lon:0 as number,
    },
    id: 0 as number,
    isJoin: false,
    filepath: '' as string,
    user: user,
    marker:[],
  },
  _setMarker(lat: number, lng: number) {
    const marker = {
        id: 0,
        width: '30px',
        height: '40px',
        latitude: lat,
        longitude: lng,
        label: {
            content: '签到点',  //文本
            color: '#FF0202',  //文本颜色
            borderRadius: 3,  //边框圆角
            borderWidth: 1,  //边框宽度
            borderColor: '#FF0202',  //边框颜色
            bgColor: '#ffffff',  //背景色
            padding: 5,  //文本边缘留白
            textAlign: 'center'  //文本对齐方式。有效值: left, right, center
        },
    }
    this.setData({ ['marker[0]']: marker });
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    var id = options.id;
    this.setData({
      id: id,
    });
    GetActivityDetail({ s_code: user.code, a_id: id }).then((res: any) => {
      this.setData({
        isJoin: res.data.data.join,
        content:res.data.data.data,
      });
      this._setMarker(res.data.data.data.latitude,res.data.data.data.longitude);
    });
  },

  //参与活动
  onJoin: async function () {
    let self = this;
    let content=this.data.content;
    await JoinActivity({ a_id: this.data.content.id, s_code: user.code}).then((res: any) => {
      if (res.data.data == '报名人数已满') {
        msg.show('人数已满', '感谢你的报名');
      }
      else {
        msg.show('参与成功', '再次点击取消参与');
        content.count+=1;
        self.setData({
          isJoin: true,
          content:content,
        });
      }
    });
  },

  //取消参加
  onCancelJoin: async function () {
    let self = this;
    let content=this.data.content;
    if (this.data.content.need == 1) {
      msg.show('该活动不能取消参与', '');
      return;
    }
    await CancelJoin({ s_code: user.code, a_id: this.data.content.id }).then(() => {
      msg.show('取消成功', '点击即可参与');
      content.count-=1;
      self.setData({
        isJoin: false,
        content:content,
      });
    })
  },


  //返回操作
  onClickLeft() {
    if (this.data.content.need == 1) {
      if (this.data.isJoin) {
        wx.navigateBack();
      }
      else {
        msg.show('该活动需要参加', '请点击参与');
      }
    }
    else {
      wx.navigateBack();
    }
  },

  //下载参与学生数据
  onStudentData: async function () {
    await GetStudentData({ a_id: this.data.content.id }).then((res: any) => {
      this.setData({
        filepath: res.data.data
      })
    })
    // 下载文件
    wx.downloadFile({
      url: this.data.filepath, // 文件的远程地址
      success: function (res) {
        let tempFilePath = res.tempFilePath; // 下载到的临时文件路径
        msg.show('下载成功', '请勿重复点击');
        // 保存文件到本地
        //wx.saveFileToDisk({
        //  filePath: tempFilePath,
        //  success: function (res:any) {
        //    var savedFilePath = res.savedFilePath; // 保存到本地的文件路径
        //    console.log('文件保存成功，路径：', savedFilePath);

        // 打开文件
        wx.openDocument({
          filePath: tempFilePath,
          success: function (res) {
            console.log('打开文件成功', res);
          },
          fail: function (res) {
            console.log('打开文件失败', res);
          }
        });
      },
      fail: function (res: any) {
        console.log('保存文件失败', res);
      }
    });
  },
  exportJoin:async function(){
      msg.Loading('导出中');
    await activityExportJoin({fileName:this.data.content.title+'报名名单',data:{a_id:this.data.content.id,openid:user.openid}})
    msg.hideLoading();
    },
    exportSign:async function(){
        msg.Loading('导出中');
      await activityExportSign({fileName:this.data.content.title+'签到情况',data:{a_id:this.data.content.id,openid:user.openid}})
      msg.hideLoading();
      }

})